/*
 * $Header:
 * /home/kant/Lehre/SS05/robocup/r2d/Robocup/soccerscope/src/main/java/soccerscope/view/layer/AgentLogLayer.java,v
 * 1.2 2006/11/05 13:57:59 wetzker Exp $
 */

package soccerscope.view.layer;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.AbstractButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import soccerscope.model.Param;
import soccerscope.model.agentplan.AgentPlanCommand;
import soccerscope.view.FieldPane;

public class AgentLogLayer
    extends Layer {

  private int mask = 8;

  public AgentLogLayer(
      FieldPane fieldPane,
      boolean enable) {

    super(fieldPane, enable);
  }

  @Override
  public String getLayerName() {

    return "AgentLog";
  }

  @Override
  public JPanel getConfigPanel() {

    JPanel panel = new JPanel();

    for (int i = 0; i < 5; i++) {
      JToggleButton lv = new JToggleButton(String.valueOf((i + 1)));
      lv.addActionListener(new AgentLogLayerActionListener());
      if ((this.mask & (1 << i + 1)) != 0) {
        lv.setSelected(true);
      }
      panel.add(lv);
    }
    return panel;
  }

  public int getLevelMask() {

    return this.mask;
  }

  public void setLevelMask(int mask) {

    this.mask = mask;
    this.fieldPane.repaint();
  }

  public void setLevel(int level) {

    this.mask ^= 1 << level;
    this.fieldPane.repaint();
  }

  @Override
  public void draw(Graphics g) {

    if (!this.enable) {
      return;
    }

    for (int i = 0; i < Param.MAX_PLAYER; i++) {
      if (this.fieldPane.getAgentPlan(i) == null) {
        continue;
      }

      ArrayList list = this.fieldPane.getAgentPlan(i);
      Iterator it = list.iterator();
      while (it.hasNext()) {
        AgentPlanCommand apc = (AgentPlanCommand) it.next();
        if ((apc.getLevel() & this.mask) != 0) {
          apc.execute(g, this);
        }
      }
    }
  }

  class AgentLogLayerActionListener
      implements ActionListener {

    public void actionPerformed(ActionEvent ie) {

      String label = ((AbstractButton) ie.getSource()).getText();
      int level = Integer.parseInt(label);
      AgentLogLayer.this.setLevel(level);
    }
  }
}
